package com.czf.testmodule.io.nio;

import org.junit.Test;

import java.nio.ByteBuffer;

/**
 * @author zfChen
 * @create 2022/6/13 15:48
 */
public class BufferTest {

    @Test
    public void test1() {
        ByteBuffer buffer = ByteBuffer.allocate(20);
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("=================================");

        String str1 = "a";
        buffer.put(str1.getBytes());
        System.out.println(buffer.position());

        String str = "abcd123陈展帆";
        buffer.put(str.getBytes());
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("=================================");


        buffer.flip();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("=================================");

        char ch = (char) buffer.get();
        System.out.println(ch);
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("=================================");

//        buffer.clear();
//        System.out.println(ch);
//        System.out.println(buffer.position());
//        System.out.println(buffer.limit());
//        System.out.println(buffer.capacity());
//        System.out.println((char) buffer.get());
//        System.out.println("=================================");
        buffer.mark();
        byte[] bytes = new byte[2];
        buffer.get(bytes);
        String s = new String(bytes);
        System.out.println(s);
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("=================================");

        buffer.reset();
        System.out.println(buffer.position());
        System.out.println(buffer.limit());
        System.out.println(buffer.capacity());
        System.out.println("=================================");

        if (buffer.hasRemaining()){
            System.out.println(buffer.remaining());
        }
        System.out.println(buffer.isDirect());
    }

}
